package com.macro.mall.controller;


import com.macro.mall.common.api.CommonResult;
import com.macro.mall.entity.Credit;
import com.macro.mall.entity.LoanPackage;
import com.macro.mall.service.LoanPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/package")
@Api(tags = "LoanPackageController", description = "我能贷")
public class LoanPackageController {

    @Autowired
    private LoanPackageService loanPackageService;

    @ApiOperation("分页我能贷查询记录")
    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    public CommonResult selectList(String name, Integer page, Integer size) {
        List<LoanPackage> creditList = null;
        try {
            creditList = loanPackageService.find(name, page, size);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(creditList);
    }

    @ApiOperation("添加我能贷")
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public CommonResult addOne(@RequestBody LoanPackage loanPackage) {
        try {
            loanPackageService.saveNotNull(loanPackage);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("添加成功");
    }

    @ApiOperation("删除我能贷")
    @RequestMapping(value = "/delOne", method = RequestMethod.POST)
    public CommonResult delOne(Integer id) {
        try {
            loanPackageService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("修改我能贷")
    @RequestMapping(value = "/upDataOne", method = RequestMethod.POST)
    public CommonResult upDataOne(@RequestBody LoanPackage loanPackage) {
        try {
            loanPackageService.updateNotNullById(loanPackage);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("修改成功");
    }
}
